package server.order;

import java.sql.ResultSet;
import java.sql.SQLException;

public class LimitOrder extends Order {

	/**
	 * Create a LimitOrder from its ID
	 * @param id	The orders ID
	 * @throws SQLException
	 */
	public LimitOrder(int id) throws SQLException {
		super(id);
	}
	
	/**
	 * Create a LimitOrder from a ResultSet
	 * @param rs	ResultSet containing order
	 * @throws SQLException
	 */
	public LimitOrder(ResultSet rs) throws SQLException {
		super(rs);
	}

	/**
	 * Create a new LimitOrder based on all of its parameters
	 * @param cur1		Base currency
	 * @param cur2		Quote currency
	 * @param rate		Order rate
	 * @param amount	Order amount
	 * @param bType		Buy or sell
	 * @param userId	User ID
	 */
	public LimitOrder(String cur1, String cur2, float rate, float amount, int bType, int userId) {
		super(cur1, cur2, rate, amount, bType, userId, ORD_LIMIT, 0, false);
	}
	
	/**
	 * Create a new LimitOrder based on all possible parameters
	 * @param cur1		Base currency
	 * @param cur2		Quote currency
	 * @param rate		Order rate
	 * @param amount	Order amount
	 * @param bType		Buy or sell
	 * @param userId	User ID
	 * @param split		ID from which the order was split
	 * @param switched	Whether or not the order is to appear switched {@link #switchCurs()}
	 */
	protected LimitOrder(String cur1, String cur2, float rate, float amount, int bType, int userId, int split, boolean switched) {
		super(cur1, cur2, rate, amount, bType, userId, ORD_LIMIT, split, switched);
	}
	
	/**
	 * Check to see if another order can satisfy this one
	 * @param order		Order to check
	 * @return			True if order can satisfy this one
	 */
	public boolean canSatisfy(Order order) {
		if (typeCheck(order) && canClose()) {
			if (getBType() == BUY) {
				return (getRate() >= order.getRate());
			} else {
				return (getRate() <= order.getRate());
			}
		} else
			return false;
	}
}
